package top.api.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import top.api.common.ResponseResult;
import top.api.domain.dto.AddArticleDto;
import top.api.domain.dto.UpdateArticleDto;
import top.api.service.ArticleService;

@RestController
@RequestMapping("/content/article")
@Api(tags = "文章")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 写博文
     * @param article
     * @return
     */
    @PostMapping
    @ApiOperation(value = "写博文")
    @PreAuthorize("@ps.hasPermission('content:article:writer')") // 写博文权限校验
    public ResponseResult add(@RequestBody AddArticleDto article){
        return articleService.add(article);
    }


    /**
     * 获取文章列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "获取文章列表")
    @PreAuthorize("@ps.hasPermission('content:article:list')") // 文章管理权限校验
    public ResponseResult list(@RequestParam Integer pageNum, @RequestParam Integer pageSize, String title,String summary){
        return articleService.getlistPage(pageNum,pageSize,title,summary);
    }

    /**
     * 获取文章详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "获取文章详情")
    @PreAuthorize("@ps.hasPermission('content:article:list')") // 文章管理权限校验
    public ResponseResult getArticleInfo(@PathVariable Long id){
        return articleService.getArticleInfo(id);
    }

    @PutMapping
    @ApiOperation(value = "修改文章")
    @PreAuthorize("@ps.hasPermission('content:article:list')") // 文章管理权限校验
    public ResponseResult updateArticle(@RequestBody UpdateArticleDto updateArticleDto){
        return articleService.updateArticle(updateArticleDto);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除文章")
    @PreAuthorize("@ps.hasPermission('content:article:list')") // 文章管理权限校验
    public ResponseResult removeArticle(@PathVariable Long id){
        return articleService.removeArticle(id);
    }
}
